#!/bin/bash
# 适用于AlmaLinux8/RockyLinux8纯净系统

######

echo_red() {
    echo -e "\033[31;40m$1\033[0m"
}

open_firewall_ports() {
    echo_red "--- 配置Firewall ---"
    if [[ $(firewall-cmd --state) == "running" ]]; then
        local ports=$1
        for i in ${ports[*]}
        do
            firewall-cmd --zone=public --add-port=$i/tcp --permanent
        done
        firewall-cmd --reload
        echo_red "当前开放端口："
        firewall-cmd --zone=public --list-ports
    fi
}

safe_oper() {
    setenforce 0
    sed -i 's/SELINUX=enforcing/SELINUX=permissive/' /etc/selinux/config
    [ -s /etc/ld.so.preload ] && mv /etc/ld.so.preload{,.bak}
    touch /etc/ld.so.preload && chmod 444 /etc/ld.so.preload
}

install_python3() {
    echo_red "--- 安装Python 3.9 ---"
    dnf -y install python39 python39-libs python39-devel python39-pip python39-pip-wheel
    python3.9 -m pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
    python3.9 -m pip config set global.format columns
    python3.9 -m pip install -U pip==22.3.1 wheel
    python3.9 -m pip install --user certifi lxml urllib3==1.26.18 requests==2.32.3
    echo_red "--- 安装Python3完毕 ---"
}

install_jdk8() {
    echo_red "--- 安装OpenJDK 1.8 ---"
    dnf -y install java-1.8.0-openjdk
    java -version
    echo_red "--- 安装OpenJDK完毕 ---"
}

install_nginx() {
    echo_red "--- 安装Nginx 1.22 ---"
    dnf -y module install nginx:1.22
    if [ -f /usr/lib/systemd/system/nginx.service ]; then
        systemctl enable nginx.service
        systemctl start nginx.service
        echo_red "--- 安装Nginx完毕 ---"
    fi
}

install_redis() {
    echo_red "--- 安装Redis 6.0 ---"
    dnf -y module install redis:6
    if [ -f /usr/lib/systemd/system/redis.service ]; then
        systemctl enable redis.service
        systemctl start redis.service
        echo_red "--- 安装Redis完毕 ---"
    fi
}

install_mysql() {
    echo_red "--- 安装MySQL 8.0 ---"
    dnf -y module install mysql:8.0
    if [ -f /usr/lib/systemd/system/mysqld.service ]; then
        systemctl enable mysqld.service
        systemctl start mysqld.service
        echo_red "--- 安装安装MySQL完毕 ---"
    fi
}

install_docker() {
    echo_red "--- 安装Docker ---"
    wget https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
    mv docker-ce.repo /etc/yum.repos.d/
    dnf -y install docker-ce

    mkdir -p /etc/docker
    cat > /etc/docker/daemon.json <<EOF
{
    "registry-mirrors": [
        "https://docker.m.daocloud.io",
        "https://docker.1panel.live"
    ] 
}
EOF

    if [ -f /usr/lib/systemd/system/docker.service ]; then
        systemctl enable docker.service
        systemctl start docker.service
        echo_red " --- 安装Docker完毕 ---"
    fi
}

######

if [ "$UID" -ne 0 ] ; then
    echo_red "请以root权限运行"
    exit 1
fi

echo "当前操作系统为：$(cat /etc/os-release | grep PRETTY_NAME | cut -d = -f 2)"
if [ "$(cat /etc/os-release | grep VERSION_ID  | cut -c 13-13)" != "8" ]; then
    echo_red " 仅支持RHEL8/AlmaLinux8/RockyLinux8纯净系统" 
    exit 1
fi

# 切换源到中科大的镜像，仅适用于RockyLinux8
if [ -f /etc/rocky-release ] && \
    [ "$(cat /etc/rocky-release | cut -c 1-21)" == "Rocky Linux release 8" ]; then
    sed -e 's|^mirrorlist=|#mirrorlist=|g' \
    -e 's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://mirrors.ustc.edu.cn/rocky|g' \
    -i.bak \
    /etc/yum.repos.d/Rocky-AppStream.repo \
    /etc/yum.repos.d/Rocky-BaseOS.repo \
    /etc/yum.repos.d/Rocky-Extras.repo \
    /etc/yum.repos.d/Rocky-PowerTools.repo
fi

echo_red "正在安装服务器软件..."
dnf -y install dnf-plugins-core epel-release iproute wget unzip

safe_oper

install_python3

read -p "Press [Y/N] key to install Nginx..." Nginx_Yes
Nginx_Yes=${Nginx_Yes:-Y}
[ $Nginx_Yes == "Y" -o $Nginx_Yes == "y" ] && install_nginx

read -p "Press [Y/N] key to install OpenJDK..." OpenJDK_Yes
OpenJDK_Yes=${OpenJDK_Yes:-Y}
[ $OpenJDK_Yes == "Y" -o $OpenJDK_Yes == "y" ] && install_jdk8

read -p "Press [Y/N] key to install Docker..." Docker_Yes
Docker_Yes=${Docker_Yes:-Y}
[ $Docker_Yes == "Y" -o $Docker_Yes == "y" ] && install_docker

read -p "Press [Y/N] key to install MySQL..." MySQL_Yes
MySQL_Yes=${MySQL_Yes:-Y}
[ $MySQL_Yes == "Y" -o $MySQL_Yes == "y" ] && install_mysql

read -p "Press [Y/N] key to install Redis..." Redis_Yes
Redis_Yes=${Redis_Yes:-Y}
[ $Redis_Yes == "Y" -o $Redis_Yes == "y" ] && install_redis

# 配置防火墙
# 定义需开放的端口，按需开放
allow_ports=(22 80 443 3306 8000 8080)
open_firewall_ports "${allow_ports[*]}"

echo "--- 初始化完成，谢谢使用！ ---"